// pages/oANavPage/oANavPage.js
import { getOANotReadNum } from "../../service/manager_service"
import { queryOAMenuList } from "../../service/config"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    oaList: [
      // {
      //   title: "房源管理",
      //   icon: "https://saas.img.ylyc.top/mine/mfygl_icon.png",
      //   navUrl: "/newHousePackage/pages/houseManagerList/houseManagerList",
      //   num: 0,
      // },
      // {
      //   title: "预约管理",
      //   icon: "https://saas.img.ylyc.top/mine/myygl_icon.png",
      //   navUrl: "/formPackage/pages/appointment/appointManagerList/appointManagerList",
      //   num: 0,
      // },
      // {
      //   title: "报备管理",
      //   icon: "https://saas.img.ylyc.top/mine/mbbgl_icon.png",
      //   navUrl: "/firstPackage/pages/reportModule/reportManage/reportManage",
      //   num: 0,
      // },
      // {
      //   title: "报备统计",
      //   icon: "https://saas.img.ylyc.top/icon/bbtjjjj_icon.jpg",
      //   navUrl: "/firstPackage/pages/reportModule/reportStatistics/reportStatistics",
      //   num: 0,
      // },
      // {
      //   title: "招聘管理",
      //   icon: "https://saas.img.ylyc.top/mine/mzpgl_icon.png",
      //   navUrl: "/formPackage/pages/invite/inviteManagerList/inviteManagerList",
      //   num: 0,
      // },
      // {
      //   title: "商务管理",
      //   icon: "https://saas.img.ylyc.top/mine/mswgl_icon.png",
      //   navUrl: "/firstPackage/pages/busJoinManagerList/busJoinManagerList",
      //   num: 0,
      // },
      // {
      //   title: "佣金设置",
      //   icon: "https://saas.img.ylyc.top/mine/myjsz_icon.png",
      //   navUrl: "/formPackage/pages/commissionManager/commissionHouseList/commissionHoustList",
      //   num: 0,
      // },

    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryOAMenuList();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.getOANotReadNum();
  },
  //获取OA菜单权限列表:
  queryOAMenuList() {
    wx.showLoading();
    queryOAMenuList().then(res => {
      wx.hideLoading();
      console.log("JJLog:菜单列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          oaList: res.data || []
        })
        if (res.data && res.data.length > 0) {
          this.getOANotReadNum();
        } else {
          wx.showToast({
            title: '您当前暂无OA权限，请联系管理员开通',
            icon: "none",
            duration: 3000
          })
        }

      }

    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryOAMenuList", err);
      setTimeout(() => {
        wx.navigateBack({
          delta: 0,
        })
      }, 2000);
    })
  },
  //跳转到详情:
  pushToPage(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: item.path,
    })
  },
  getOANotReadNum() {
    getOANotReadNum().then(res => {
      console.log("JJLog:OA未读数量是:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data) {
          //动态数组:
          // let houseManager = 'oaList[' + 0 + '].num';
          // let bookingManager = 'oaList[' + 1 + '].num';
          // let recommendManager = 'oaList[' + 2 + '].num';
          // let findJobManager = 'oaList[' + 4 + '].num';
          // let businessManager = 'oaList[' + 5 + '].num';
          // this.setData({
          //   [houseManager]: res.data.houseManager || 0,
          //   [bookingManager]: res.data.bookingManager || 0,
          //   [recommendManager]: res.data.recommendManager || 0,
          //   [findJobManager]: res.data.findJobManager || 0,
          //   [businessManager]: res.data.businessManager || 0,
          // })
          let arr = this.data.oaList;
          let tempArr = [];
          for (let key in res.data) {
            let obj = {
              name: key,
              num: res.data[key] || 0
            }
            tempArr.push(obj)
          }

          arr.forEach((i) => {
            tempArr.forEach((j) => {
              if (i.menuKey == j.name) {
                i.num = j.num
              }
            })
          })


          // arr.forEach((i) => {
          //   if (i.name == "新房管理") {
          //     i.num = res.data.houseManager || 0
          //   }
          //   if (i.name == "预约管理") {
          //     i.num = res.data.bookingManager || 0
          //   }
          //   if (i.name == "报备管理") {
          //     i.num = res.data.recommendManager || 0
          //   }
          //   if (i.name == "招聘管理") {
          //     i.num = res.data.findJobManager || 0
          //   }
          //   if (i.name == "商务管理") {
          //     i.num = res.data.businessManager || 0
          //   }
          //   if (i.name == "公司审核") {
          //     i.num = res.data.companyExamine || 0
          //   }
          //   if (i.name == "经纪人审核") {
          //     i.num = res.data.agentExamine || 0
          //   }
          //   if (i.name == "佣金管理") {
          //     i.num = res.data.commissionExamine || 0
          //   }
          // })
          this.setData({
            oaList: arr
          })
        }
      }
    }).catch(err => {
      console.error("JJLog:getOANotReadNum", err);
    })
  },

})